package org.refresh.groovy;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import org.refresh.core.intelligence.Constraint;
import org.refresh.core.intelligence.ConstraintEvaluator;

/******************************************************************************
 * Copyright (c) 2005, 2006 Jules White.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jules White - initial API and implementation 
 ****************************************************************************/
public class GroovyEvaluator implements ConstraintEvaluator {

	public boolean evaluate(Object src, Object trg, Constraint constraint) {
		GroovyConstraint gc = (GroovyConstraint)constraint;
		Object val = null;

		Binding i = new Binding();

		i.setVariable("self", src); // Set
		i.setVariable("target", trg);
		GroovyShell shell = new GroovyShell( i);

		val = shell.evaluate(gc.getExpression());

		if(val instanceof Boolean){
			return ((Boolean)val).booleanValue();
		}
		else{
			return val != null;
		}
	}

}
